import { request } from '@umijs/max';
import { uuid } from './utils';

export type CaptchaRes = {
  token: string;
  secretKey: string;
  originalImageBase64: string;
  jigsawImageBase64: string;
};

export type CheckCaptchaRes = {
  token: string;
};

export type CheckCaptchaReq = {
  clientUid: string;
  token: string;
  pointJson: string;
  ts: number;
};

/**
 * 异步获取验证码图片
 *
 * 本函数通过发送HTTP请求获取验证码图片数据，并返回解析后的数据
 * 主要用于在用户登录或注册时获取验证码图片，以提高系统安全性
 *
 */
export async function getPicture() {
  return request<CaptchaRes>('/auth/generate', {
    method: 'GET',
    params: {
      clientUid: uuid(),
    },
  });
}

/**
 * 异步获取验证验证码
 */
export async function checkCaptcha(body: CheckCaptchaReq) {
  return request<CheckCaptchaRes>('/auth/verify', {
    method: 'POST',
    data: body,
  });
}
